/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | ztree功能模块封装
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'configure', 'element', 'allHeight', 'sessionFun'], function (exports) {
    var $ = layui.jquery;
    var configure = layui.configure;
    var element = layui.element;
    var allHeight = layui.allHeight;
    var sessionFun = layui.sessionFun;
    var ztree_Menu = {};
    var curLocation = '';
    var ztree = {
        param: {
            multiCheck: true,//是否多选
            idKey: '',//父级id
            pIdKey: '',//子级中与父级id关联
            rootPId: '', //根节点id
            onClick: null,
            checkData: []//默认被选中数据
        },
        deepLevel: 0,
        //字体样式
        getFont: function (treeId, node) {
            if (!node.isParent) {
                if (!node.current) {
                    return {
                        'font-weight': 'bold',
                        // 'text-decoration': 'line-through'
                    };
                }
            }
            var font = {
                'font-weight': 'bold'
            };
            return font;
        },
        //用于冻结ztree根节点
        dblClickExpand: function (treeNode) {
            return treeNode.level > 1;
        },
        init: function (id, showNode, data) {
            setting.data.simpleData.idKey = ztree.param.idKey;
            setting.data.simpleData.pIdKey = ztree.param.pIdKey;
            setting.data.simpleData.rootPId = ztree.param.rootPId;
            setting.check.enable = ztree.param.multiCheck;
            window.treeobj = $.fn.zTree.init($('#' + id), setting, data);
            // fixIcon(id);//修正ztree图标，让文件节点显示文件夹图标
            // treeobj.expandAll(true); //设定所有节点展开
            ztree_Menu = $.fn.zTree.getZTreeObj(id);
            //获取所有的节点
            var checkeds = ztree_Menu.getNodes();
            //遍历所有节点
            $(checkeds).each(function (index, item) {

                $(ztree.param.checkData).each(function (initIndex, initItem) {
                    if (item.menuId == initItem) {
                        var node = ztree_Menu.getNodeByParam('menuId', initItem); //根据专业名称获取节点，用于展开该节点
                        // ztree_Menu.selectNode(node, true);
                        ztree_Menu.checkNode(node, true, true); //指定选中ID的节点
                    }
                });

                $(item.children).each(function (childIndex, childItem) {
                    $(ztree.param.checkData).each(function (initIndex, initItem) {
                        if (childItem.menuId == initItem) {
                            var node = ztree_Menu.getNodeByParam('menuId', initItem); //根据专业名称获取节点，用于展开该节点
                            // ztree_Menu.selectNode(node, true);
                            ztree_Menu.checkNode(node, true, true); //指定选中ID的节点
                        }
                    });
                });
            });
            // var node = ztree_Menu.getNodeByParam('name', showNode); //根据专业名称获取节点，用于展开该节点
            // ztree_Menu.selectNode(node, true); //指定选中ID的节点
            // ztree_Menu.expandNode(node, true, false); //指定选中ID节点展开
        },
        //标签页添加
        tabAdd: function (url, id, name) {
            //新增一个Tab项 传入三个参数，分别对应其标题，tab页面的地址，还有一个规定的id，是标签中data-id的属性值
            //关于tabAdd的方法所传入的参数可看layui的开发文档中基础方法部分
            element.tabAdd('demo', {
                title: name,
                content: '<iframe class="dwgIframe" data-id="' + id + '" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;vertical-align:middle;"></iframe>',
                id: id //规定好的id
            });
            allHeight.init('.layui-tab-title', '.layui-tab-content');
        },
        //标签页切换
        tabChange: function (id) {
            //切换到指定Tab项
            element.tabChange('demo', id); //根据传入的id传入到指定的tab项
        }
    };

    //修正zTree的图标，让文件节点显示文件夹图标
    function fixIcon(id) {
        var treeObj = $.fn.zTree.getZTreeObj(id);
        var nodes = treeObj.getNodes();
        //过滤出sou属性为true的节点（也可用你自己定义的其他字段来区分，这里通过sou保存的true或false来区分）
        for (j = 0; j < nodes.length; j++) {//遍历目录节点，设置isParent属性为true;
            nodes[j].isParent = true;
        }
        treeObj.refresh();//调用api自带的refresh函数。
    }

    function zTreeOnCheck(event, treeId, treeNode) {
        var current = treeNode;//当前项
        ztree.param.checkData = arrDeal(ztree.param.checkData, current);
    }

    function arrDeal(arr, current) {
        var ifReply = false;
        $(arr).each(function (index, item) {
            if (item == current.menuId) {
                arr.splice(index, 1);
                ifReply = true;
                return false;
            }
        });
        if (!ifReply) {
            arr.push(current.menuId);
        }
        return arr;
    }


    //配置信息
    var setting = {
        check: {
            enable: true
        },
        view: {
            nameIsHTML: true,
            fontCss: ztree.getFont,
            showTitle: false,
            dblClickExpand: ztree.dblClickExpand
        },
        data: {
            simpleData: {
                enable: true,
                idKey: '',
                pIdKey: '',
                rootPId: ''
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode) {
                var nodes = ztree_Menu.getSelectedNodes();
                curLocation = '';
                if (nodes.length > 0) {
                    var allNode = nodes[0].name;//获取当前选中节点
                    var node = nodes[0].getParentNode();

                    getParentNodes(node, allNode);
                }
                var location = "";
                var nodeArrs = curLocation.split("/").reverse();
                $(nodeArrs).each(function (index, item) {
                    location += '/' + item;
                });
                ztree.param.onClick(event, treeId, treeNode, location);
            },
            onCheck: zTreeOnCheck
        }
    };

    function getParentNodes(node, allNode) {
        if (node != null) {
            allNode += "/" + node.name;
            var curNode = node.getParentNode();
            getParentNodes(curNode, allNode);
        } else {
            //根节点
            curLocation = allNode;
        }
    }

    exports('ztree', ztree);
});